use std::fmt::{Debug};
use log::info;
use rpc::{DefaultLocalSeqCreator, RpcContext, RpcEvent, RpcResult};
use rpc::bootstrap::BootstrapBuilder;
use rpc_derive::{router, rpc_router};
use rpc_server::server::RpcServerBuilder;

// #[tokio::test]
async fn server_test() -> RpcResult<()>{
    let bootstrap = BootstrapBuilder::new()
        .router("/hello", router!(hello))
        .set_local_seq_creator(Box::new(DefaultLocalSeqCreator::new("router_service-demo".to_string())))
        ;
    let server = RpcServerBuilder::new(bootstrap, 8888)
        .build();
    server.start_server().await.unwrap();
    Ok(())

}

#[rpc_router]
fn hello(context: &mut RpcContext, data: i32) -> i32 {
    context.send_event(Box::new(HelloEvent{})).unwrap();
    data + 100
}

// pub struct HelloRouterFactory;
// #[derive(Debug)]
// pub struct HelloRouter {
//     pub data: i32,
// }
// impl rpc::router::RouterFactory for HelloRouterFactory {
//     fn parse(&self, rpc_context: &rpc::RpcContext, byte_muts: bytes::BytesMut) -> rpc::RpcResult<Box<dyn rpc::router::DoRouter>> {
//         Ok(Box::new(HelloRouter { data: rpc::decode::<i32>(rpc_context, byte_muts.freeze().to_vec())? }))
//     }
// }
//
// impl rpc::router::DoRouter for HelloRouter {
//     fn execute(&mut self, rpc_context: &mut rpc::RpcContext) -> rpc::RpcResult<Box<dyn rpc::codec::RpcEncode>> {
//         let result = hello(rpc_context, self.data.clone());
//         rpc::handle_result!(result)
//     }
// }


#[derive(Debug)]
struct HelloEvent {}

impl RpcEvent for HelloEvent {
    fn consume(&self, context: RpcContext) -> RpcResult<()> {
        info!("received hello message");
        Ok(())
    }
}

